<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
ini_set("session.gc_maxlifetime", 300000);
ini_set("session.gc_divisor", "1");
ini_set("session.gc_probability", "1");
ini_set("session.cookie_lifetime", "0");

include_once("libs/setup.php");
include_once("libs/helper.php");

session_start();

if (check_user_session()) {
    header("Location: home.php");
} else {
    
    if (!isset($_REQUEST["submit"])) {
        SmartyValidate::connect($smarty, true);
        SmartyValidate::register_validator('required_email', 'email', 'notEmpty');
        SmartyValidate::register_validator('format_email', 'email', 'isEmail');
        SmartyValidate::register_validator('required_password', 'password', 'notEmpty');

        $smarty->display('signin.tpl');
    } else {
        SmartyValidate::connect($smarty);
       //  validate after a POST
        if (!SmartyValidate::is_valid($_POST)) {

            $smarty->assign($_POST);
            $smarty->assign('is_display_error', 'block');
            $smarty->display('signin.tpl');
        } else {

            $loginName = $_REQUEST["email"];
            $password = $_REQUEST["password"];
           
            $mysqli = connect_database($smarty);
         
          
          
            $lstUsers = $mysqli->query("Select * From user");
            if ($lstUsers) {
                $login_result = false;
                while ($row = $lstUsers->fetch_assoc()) {
                    $salt = $row["PassSalt"];
                    if ($loginName == $row["Email"] &&
                            md5($salt . $password) == $row["Password"]) {
                        if ($row["KichHoat"] == false) {
                            $smarty->assign('is_display_error', 'block');

                            $smarty->assign('error_message', 'Tài khoản của bạn chưa được kích hoạt, xin vui lòng kiểm tra email để kích hoạt tài khoản');
                            $smarty->display('signin.tpl');
                            return;
                        }

                        $login_result = true;
                        $loginName = $row["Email"];
                        $hoten = $row["HoTen"];
                        $_SESSION['ma_user'] = $row["Ma"];
                        $_SESSION['email'] = $loginName;
                        $_SESSION['hoten'] = $hoten;
                        $_SESSION['last_activity'] = time();
                        if (isset($_REQUEST["keep_session"])) {
                            $_SESSION['expire_time'] = 259200;
                        } else {
                            $_SESSION['expire_time'] = 1200;
                        }
                       SmartyValidate::disconnect();
                        header("Location: home.php");
                        return;
                    }
                }
                if ($login_result == false) {
                    $smarty->assign('is_display_error', 'block');
                    $smarty->assign('error_message', 'Email hoặc password không đúng. Xin vui lòng kiểm tra và thực hiện lại.');
                    $smarty->assign($_POST);
                    $smarty->display('signin.tpl');
                }
            }
            if($mysqli->error)
                {
                system_failed($mysqli->error,$smarty);
                }
            }
        }
    }
?>
